package com.avic.rong.fragment.balance;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.avic.rong.R;
import com.avic.rong.activity.mine.CapitalSubsidiaryActivity;
import com.avic.rong.activity.product.DetailProductActivity;
import com.avic.rong.api.mine.AccountBalanceApi;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.flyco.tablayout.SlidingTabLayout;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class AccountBalanceFragment extends Fragment {
    @BindView(R.id.btn_top_up)
    Button btn_top_up;
    @BindView(R.id.tablayout_capital)
    SlidingTabLayout mTab;
    @BindView(R.id.viewpager_capital)
    ViewPager mVp;
    @BindView(R.id.tv_balance)
    TextView tv_balance;

    private ArrayList<Fragment> fragments = new ArrayList<>();

    private String[] titles = {"近一个月", "近三个月", "近半年", "近一年","全部"};

    private Integer[] integers = {1,2,3,4,0};
    private Unbinder unbinder;
    private View view;
    private double balance;
    private String logintoken;

    public AccountBalanceFragment() {
        // Required empty public constructor
    }


    private void initData(String token) {
        AccountBalanceApi accountBalanceApi = new AccountBalanceApi((CapitalSubsidiaryActivity) getActivity(),accountBalanceListener );
        accountBalanceApi.setCache(false);
        accountBalanceApi.setArgument(token,1,10,0);
        HttpManager.getInstance().doHttpDeal(accountBalanceApi);
    }

    HttpOnNextListener<BaseResultEntity> accountBalanceListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)){
                //余额
                balance = baseResultEntity.getBalance();
                String substring = String.valueOf(balance);
                if (!substring.endsWith("0")){
                    tv_balance.setText("¥ "+substring);
                }else {
                    int integer = Double.valueOf(substring).intValue();
                    tv_balance.setText("¥ "+integer);
                }
            }else {
                Toast toast = Toast.makeText(getContext(), baseResultEntity.getMsg(), Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_account_balance, container, false);

        unbinder = ButterKnife.bind(this, inflate);


        String token = null;
        if (SharedPreferencesUtil.isLogin()) {
            logintoken = SharedPreferencesUtil.getLoginToken();
            try {
                token = URLEncoder.encode(logintoken,"UTF-8");
                initData(logintoken);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        for (int i = 0; i < titles.length; i++) {
            fragments.add(CapitalFragment.getInstance(titles[i], getContext(), integers[i],logintoken));
        }


        MyPageAdapter myPageAdapter = new MyPageAdapter(getChildFragmentManager());
        mVp.setAdapter(myPageAdapter);
        mVp.setOffscreenPageLimit(fragments.size());
        mTab.setViewPager(mVp);

        mVp.setCurrentItem(0);

        return inflate;
    }



    @OnClick(R.id.btn_top_up)
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn_top_up:
//                startActivity(TopUpActivity.queryIntent(getContext()));
                startActivity(DetailProductActivity.getIntent(getContext(), "485"));
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public class MyPageAdapter extends FragmentPagerAdapter {
        public MyPageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}
